package com.walkline.screen;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.FontManager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.walkline.app.RadioAppConfig;
import com.walkline.radio.RadioPlayer;
import com.walkline.util.Enumerations.ChannelList;
import com.walkline.util.Function;
import com.walkline.util.ui.ForegroundManager;
import com.walkline.util.ui.ListStyleButtonField;
import com.walkline.util.ui.ListStyleButtonSet;

public final class RadioScreen extends MainScreen
{
	ForegroundManager _foreground = new ForegroundManager(0);
	ListStyleButtonSet _categoriesSet = new ListStyleButtonSet();
	RadioPlayer _player = new RadioPlayer();
	LabelField _status = new LabelField(null, LabelField.USE_ALL_WIDTH);
	private RadioAppConfig _appConfig;

    public RadioScreen()
    {
    	super (NO_VERTICAL_SCROLL | USE_ALL_HEIGHT | NO_SYSTEM_MENU_ITEMS);

    	String appVer = "v" + ApplicationDescriptor.currentApplicationDescriptor().getVersion();
        setTitle(RadioAppConfig.APP_TITLE + " - " + appVer);

        setDefaultClose(false);

		try {
			FontFamily family = FontFamily.forName("BBGlobal Sans");
			Font appFont = family.getFont(Font.PLAIN, 8, Ui.UNITS_pt);
			FontManager.getInstance().setApplicationFont(appFont);
		} catch (ClassNotFoundException e) {}

		String titleIconName = Display.getWidth() < 640 ? "titleIcon_small.png" : "titleIcon_large.png";
		LabelField labelTitleField;
		BitmapField bmpTitleField = new BitmapField(Bitmap.getBitmapResource(titleIconName));
		bmpTitleField.setSpace(5, 5);
		labelTitleField = new LabelField(RadioAppConfig.APP_TITLE, LabelField.ELLIPSIS);
		labelTitleField.setFont(RadioAppConfig.FONT_MAIN_TITLE);

		HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
		VerticalFieldManager vfm = new VerticalFieldManager(FIELD_VCENTER);
		vfm.add(labelTitleField);
		hfm.add(bmpTitleField);
		hfm.add(vfm);
		hfm.setBackground(RadioAppConfig.bgColor_Gradient);
		setTitle(hfm);

		ListStyleButtonField _item;

		for (int i=0; i<ChannelList.categories.length; i++)
		{
	        _item = new ListStyleButtonField(i);
	        _item.setChangeListener(new FieldChangeListener()
	        {
				public void fieldChanged(Field field, int context)
				{
					ListStyleButtonField item = (ListStyleButtonField) _categoriesSet.getFieldWithFocus();
					UiApplication.getUiApplication().pushScreen(new ChannelListScreen(item.getChannelId(), _player));
				}
			});
	        _categoriesSet.add(_item);
		}

        _foreground.add(_categoriesSet);
        add(_foreground);

        synchronized (Application.getEventLock())
		{
			_appConfig = new RadioAppConfig();
			_appConfig.initialize();
		}
    }

    private void showRegisterScreen()
    {
    	RegisterScreen popupScreen = new RegisterScreen();
		UiApplication.getUiApplication().pushModalScreen(popupScreen);

		String code = popupScreen.getRegisterCode();

		if (popupScreen != null) {popupScreen = null;}
		if (!code.equals(""))
		{
			if (Function.getRegisterCode().equals(code))
			{
				_appConfig.setRegisterStatus(true);
				_appConfig.save();

				Function.errorDialog("注册成功，感谢你的支持！");
			} else {
				Function.errorDialog("注册码错误，请重新输入！");
			}
		}	
    }

	protected boolean keyChar(char key, int status, int time)
    {
		if (getFieldWithFocus() instanceof ForegroundManager)
		{
			switch (key)
			{
    			case Characters.SPACE:
    				_player.pause();
    				return true;
    			case Characters.LATIN_CAPITAL_LETTER_L:
    			case Characters.LATIN_SMALL_LETTER_L:
    				return true;
    			case Characters.LATIN_CAPITAL_LETTER_Q:
    			case Characters.LATIN_SMALL_LETTER_Q:
    				Function.showExitDialog();
    				return true;
			}
		}

    	return super.keyChar(key, status, time);
    }

	MenuItem menuRegister = new MenuItem("注册", 100, 10)
	{
    	public void run() {showRegisterScreen();}
    };

    MenuItem menuExit = new MenuItem("退出", 100, 20)
    {
    	public void run() {Function.showExitDialog();}
    };

	protected void makeMenu(Menu menu, int instance)
    {
		if (!_appConfig.getResisterStatus())
		{
			menu.add(menuRegister);
	    	menu.addSeparator();
		}

    	menu.add(menuExit);

    	super.makeMenu(menu, instance);
    }

	protected boolean onSavePrompt() {return true;}

	public boolean onClose()
	{
		UiApplication.getUiApplication().requestBackground();

		return true;
	}
}